package thread3;

class Test {
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (locker1) {
                // sleep 原先确保 t1和t2都分别拿到loker1和loker2后 再进行后续动作 发现死锁
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (locker2) {
                    System.out.println("加锁成功!");
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (/*locker2->*/locker1) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (/*locker1->*/locker2) {
                    System.out.println("加锁成功!");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
